<template>
  <div class="app-container">
    <!-- 添加或修改项目基础信息管理对话框 -->
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="98px">
      <el-form-item label="生产单号" prop="prodNo">
        <el-input v-model="queryParams.prodNo" placeholder="请输入生产单号" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="工程名称" prop="gongchengName">
        <el-input v-model="queryParams.gongchengName" placeholder="请输入工程名称" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="产品编码" prop="goodsCode">
        <el-input v-model="queryParams.goodsCode" placeholder="请输入产品编码" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>

      <el-form-item label="产品名称" prop="goodsName">
        <el-input v-model="queryParams.goodsName" placeholder="请输入产品名称" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>

      <el-form-item label="产品型号" prop="goodsModel">
        <el-input v-model="queryParams.goodsModel" placeholder="请输入产品型号" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>

      <!--<el-form-item label="工艺编码" prop="gongyiCode">
        <el-input v-model="queryParams.gongyiCode" placeholder="请输入工艺名称"
                  clearable @keyup.enter.native="handleQuery"/>
      </el-form-item>-->

      <el-form-item label="工序名称" prop="name">
        <el-input v-model="queryParams.name" placeholder="请输入工序名称" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="图纸号" prop="shuoming">
        <el-input v-model="queryParams.shuoming" placeholder="请输入图纸号" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
       <el-form-item label="生产产品名称" prop="gongxuName">
        <el-input v-model="queryParams.gongxuName" placeholder="请输入生产产品名称" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>

      <el-form-item label="生产产品型号" prop="gongxuModel">
        <el-input v-model="queryParams.gongxuModel" placeholder="请输入生产产品型号" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <!-- <el-form-item label="工序编码" prop="code">
        <el-input v-model="queryParams.code" placeholder="请输入工序编码"
                  clearable @keyup.enter.native="handleQuery"/>
      </el-form-item> -->
      <el-form-item label="分配人" prop="createBy">
        <el-input v-model="queryParams.createBy" placeholder="请输工人" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="工人" prop="userName">
        <el-input v-model="queryParams.userName" placeholder="请输工人" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="工人车间" prop="deptName">
        <el-select v-model="queryParams.deptName" placeholder="请选择工人车间" style="width: 100%" @change="handshopChange" clearable>
          <el-option v-for="(item, index) in workshopOptions" :key="index" :label="item.label" :value="item.id">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="报工状态" prop="status">
        <el-select v-model="queryParams.status" placeholder="报工状态" clearable style="width: 130px">
          <el-option v-for="dict in dict.type.gxbg_status" :key="dict.value" :label="dict.label" :value="dict.value" />
        </el-select>
      </el-form-item>
      <el-form-item label="分配时间" prop="createTime">
        <el-date-picker clearable v-model="queryParams.createTime" type="date" value-format="yyyy-MM-dd"
          placeholder="请选择分配时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
        <!-- <el-button icon="el-icon-refresh" size="mini" @click="modifyMoneyAll"
          v-hasPermi="['develop:WeiwanchengTask:piliangMoneyDetailBnt']">批量修改单价及工序</el-button>-->
        <!--<el-button icon="el-icon-edit" size="mini" @click="batchModifyGongxu"
          >批量修改工序</el-button>-->
      </el-form-item>
    </el-form>
    <!--<el-table :data="dataList" :max-height="tableMaxHeight" @selection-change="handleSelectionChange"
              v-loading="loading">-->

    <el-table :data="columns" ref="dragTable" row-key="id" v-loading="loading" :max-height="tableMaxHeight"
      @selection-change="handleSelectionChange" @sort-change="handleSortChange" show-summary
      :summary-method="getSummaries">
      <el-table-column align="center" type="selection" width="55" />
      <el-table-column align="center" label="序号" type="index" width="50">
        <template slot-scope="scope">
          <span>{{ (queryParams.pageNum - 1) * queryParams.pageSize + scope.$index + 1 }}</span>
        </template>
      </el-table-column>
      <el-table-column sortable label="生产单号" align="center" prop="prodNo" show-overflow-tooltip />
      <el-table-column sortable label="需求日期" align="center" prop="prodDaleDate">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.prodDaleDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
     <!-- <el-table-column sortable label="订单编号" align="center" prop="projectCode" show-overflow-tooltip />-->
       <el-table-column label="工程名称" align="center" prop="gongchengName" show-overflow-tooltip />
      <<!--el-table-column sortable label="产品编码" align="center" prop="goodsCode" />-->
      <el-table-column sortable align="center" label="报工状态" prop="status">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.gxbg_status" :value="scope.row.status" />
        </template>
      </el-table-column>
      <el-table-column sortable align="center" label="产品名称" prop="goodsName" />
      <el-table-column sortable align="center" label="产品型号" prop="goodsModel" />
      <el-table-column sortable align="center" label="工序名称" prop="name" />
      <el-table-column sortable align="center" label="生产产品名称" prop="gongxuName" />
      <el-table-column sortable align="center" label="生产产品型号" prop="gongxuModel" />
      <!--<el-table-column sortable align="center" label="工序单价" prop="money" />
      <el-table-column sortable align="center" label="工序总价" prop="allMoney" />-->
      <el-table-column sortable align="center" label="图纸号" prop="shuoming" />
      <el-table-column sortable align="center" label="分配人" prop="createBy" />
      <el-table-column sortable align="center" label="工人" prop="userName" />
      <el-table-column sortable align="center" label="工人车间" prop="deptName" />
      <el-table-column sortable align="center" label="标准工时" prop="shuoming" show-overflow-tooltip />
      <el-table-column sortable align="center" label="分配数量" prop="count" />
      <el-table-column sortable align="center" label="完成数量" prop="finishCount" />
      <el-table-column sortable align="center" label="剩余完成数量" prop="remainCount" />
      <el-table-column sortable align="center" label="分配时间" prop="hour" />
      <el-table-column sortable align="center" label="创建时间" prop="createTime" />
      <!--<el-table-column align="center" label="单价" prop="money" />
      <el-table-column sortable align="center" label="总价" prop="allMoney" />-->
      <!-- <el-table-column align="center" label="备注" prop="remark"/>-->
      <el-table-column align="center" class-name="small-padding fixed-width" label="操作" width="300px">
        <template slot-scope="scope">
          <el-button @click="modifyMoney(scope.row)" v-hasPermi="['develop:WeiwanchengTask:editMoneyBnt']"
            icon="el-icon-view" size="mini" type="text">
            修改单价
          </el-button>
          <!--<el-button @click="baogong(scope.row)" v-if="scope.row.type=='0' && scope.row.remainCount>0" v-hasPermi="['develop:WeiwanchengTask:workBnt']" icon="el-icon-view" size="mini"
                     type="text">
            计件报工
          </el-button>
          <el-button @click="baogongJishi(scope.row)" v-if="scope.row.type=='1' && scope.row.remainCount>0"  v-hasPermi="['develop:WeiwanchengTask:workBnt']" icon="el-icon-view" size="mini"
                     type="text">
            工时报工
          </el-button>-->
          <el-button @click="baogongMingxi(scope.row)" v-hasPermi="['develop:WeiwanchengTask:workDetailBnt']"
            icon="el-icon-view" size="mini" type="text">
            报工明细
          </el-button>
          <el-popconfirm title="确认删除该条的数据项？" @confirm="handleDelete(scope.row)">
            <el-button slot="reference" type="text" plain icon="el-icon-delete" size="mini" >删除派工
            </el-button>
          </el-popconfirm>
          <!--<el-button @click="zhuanpai(scope.row)" v-hasPermi="['develop:WeiwanchengTask:redeployBnt']" icon="el-icon-view" size="mini"
                     type="text">
            转派
          </el-button>-->
        </template>
      </el-table-column>
    </el-table>
    <modifyMoney-form @ok="getList" ref="modifyMoneyForm"></modifyMoney-form>
    <modifyMoneyAll-form @ok="getList" ref="modifyMoneyAll"></modifyMoneyAll-form>
    <baogongForm-list @ok="getList" ref="baogongForm"></baogongForm-list>
    <baogongForm-jishi-list @ok="getList" ref="baogongJishiForm"></baogongForm-jishi-list>
    <baogongMingxigForm-list @ok="getList" ref="baogongMingxigForm"></baogongMingxigForm-list>
    <zhuanpai-form @ok="getList" ref="zhuanpaiForm"></zhuanpai-form>
    <goods-sale-do-gongxu-form-all @ok="getList" ref="batchModifyGongxuForm"></goods-sale-do-gongxu-form-all>
    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
      @pagination="getList" />
  </div>
</template>

<script>

import { addAction, deleteAction, downloadFile, getAction, getById, accAdd } from '@/api/manage'
import ModifyMoneyForm from "@/views/develop/projectBaseInfo/prjForGoodsIn/ModifyMoneyForm";
import ModifyMoneyAllForm from "@/views/develop/projectBaseInfo/prjForGoodsIn/ModifyMoneyAllForm";
import BaogongFormList from "@/views/develop/projectBaseInfo/prjForGoodsIn/BaogongFormList";
import BaogongFormJishiList from "@/views/develop/projectBaseInfo/prjForGoodsIn/BaogongFormJishiList";
import BaogongMingxigFormList from "@/views/develop/projectBaseInfo/prjForGoodsIn/BaogongMingxigFormList";
import ZhuanpaiForm from "@/views/develop/projectBaseInfo/prjForGoodsIn/ZhuanpaiForm";
import PaigongNoProjectForm from "@/views/develop/projectBaseInfo/prjForGoodsIn/PaigongNoProjectForm";
import GoodsSaleDoGongxuFormAll from "@/views/develop/lnGongxu/module/GoodsSaleDoGongxuFormAll.vue";

export default {
  name: "TaskAllDoList",
  dicts: ['isdo', 'gxbg_status', 'goods_status'],
  components: {
    BaogongFormList,
    BaogongFormJishiList,
    BaogongMingxigFormList,
    ZhuanpaiForm,
    PaigongNoProjectForm,
    ModifyMoneyForm,
    ModifyMoneyAllForm,
    GoodsSaleDoGongxuFormAll,
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      activeName: "first",
      // 弹出层标题
      title: "已完成任务",
      // 是否显示弹出层
      open: false,
      // 显示搜索条件
      showSearch: true,
      single: true,
      multiple: true,
      // 表单参数
      form: {},
      ids: [],
      goodsIds: [],
      proId: "",
      proStatus: "",
      columns: [],
      total: 0,
      // 表单校验
      rules: {},
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        params: {},
        id: null,
        prodNo: null,
        shuoming: null,
        projectNo: null,
        gongyiCode: null,
        gongyiName: null,
        goodsName: null,
        goodsCode: null,
        projectCode: null,
        name: null,
        code: null,
        userName: null,
        deptId: null,
        status: '2',
        ongxuName:null,
        gongxuModel:null,
        deptName: this.$store.getters.deptName,
        //createBy: this.$store.getters.name,
        //remainCount: '0',
      },
      url: {
        list: '/goodsSaledoGongxuUser/goodsSaledoGongxuUser/list',
        getById: '/projectBaseInfo/projectBaseInfo/',
        exportData: '/goodsSale/goodsSale/exportData',
        userTreeSelect: '/system/dept/userTreeSelect',
        deleteGxUser: '/goodsSaledoGongxuUser/goodsSaledoGongxuUser/deleteGxUser/',
      },
      workshopOptions: [
      ],
      tableMaxHeight: window.innerHeight - 320,
    };
  },
  created() {
    this.getList();
    this.getUser();
    window.addEventListener('resize', this.getTableHeight);
  },
  methods: {
    handleDelete(row){
      const ids = row.id ;
      deleteAction(this.url.deleteGxUser,ids ).then(response => {
        this.getList();
        this.$modal.msgSuccess("删除成功！");
      }).catch(() => {
      });
    },
    handshopChange(e) {
      // 查找匹配的选项，不仅匹配value，也匹配id
      const selected = this.workshopOptions.find(item => item.value === e || item.id === e);
      if (selected) {
        // 设置表单的部门ID和名称
        // this.queryParams.deptId = selected.id;
        this.queryParams.deptName = selected.label;
      }
    },
    getUser() {
      let that = this;
      getAction(that.url.userTreeSelect, { parentId: '57d779853f0f4d14b03d51db30099456' }).then(res => {
        that.workshopOptions = res.data;
      })
    },
    getSummaries(param) {
      const { columns, data } = param;
      const sums = [];
      columns.forEach((column, index) => {
        if (index === 0) {
          sums[index] = "合计";
          return;
        }
        const values = data.map((item) => Number(item[column.property]));
        // 只合计amount1和amount3，想合并哪些列就在这里指定
        if (column.property === "count" || column.property === "finishCount" || column.property === "allMoney" || column.property === "remainCount") {
          sums[index] = values.reduce((prev, curr) => {

            const value = Number(curr);
            if (!isNaN(value)) {
              return accAdd(prev, curr);
            } else {
              return prev;
            }
          }, 0);
        }
      });
      return sums;
    },
    /** 排序触发事件 */
    handleSortChange(column, prop, order) {
      this.queryParams.orderByColumn = column.prop;
      this.queryParams.isAsc = column.order;
      this.handleQuery();
    },
    modifyMoney(row) {
      this.$refs.modifyMoneyForm.show(row);
    },
    modifyMoneyAll() {
      const ids = this.ids;
      if (ids == null || ids == '' || ids == 'undefined') {
        this.$modal.msgError("请选择一条记录");
      } else {
        this.$refs.modifyMoneyAll.show(ids);
      }
    },
    baogong(row) {
      this.$refs.baogongForm.baogongShow(row);
    },
    baogongJishi(row) {
      this.$refs.baogongJishiForm.baogongJishiShow(row);
    },
    baogongMingxi(row) {
      this.$refs.baogongMingxigForm.baogongMingxigShow(row);
    },
    zhuanpai(row) {
      this.$refs.zhuanpaiForm.zhuanpaigShow(row);
    },
    xunjian(row) {
      this.$refs.xunjianForm.xunjianShow(row);
    },
    taskFp(row) {
      this.$refs.taskFpForm.taskFpShow(row);
    },
    chaijie(row) {
      this.$refs.chaijieForm.chaijieGoods(row);
    },
    choseGy(row) {
      this.$refs.choseGxForm.choseGy(row);
    },
    getList() {
      this.loading = true;
      getAction(this.url.list, this.queryParams).then(response => {
        this.columns = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
      this.selectRows = selection;
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    reset() {
      this.form = {
        id: null,
        remark: null,
        delFlag: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null
      };
      this.resetForm("form");
    },

    handleExportDetail(row) {
      downloadFile(this.url.exportData, this.queryParams);
    },

    /**
     * 批量修改工序
     */
    batchModifyGongxu() {
      const ids = this.ids;
      if (ids == null || ids == '' || ids == 'undefined' || ids.length === 0) {
        this.$modal.msgError("请选择至少一条记录");
        return;
      }

      // 获取选中的行数据
      const selectedRows = this.columns.filter(row => ids.includes(row.id));

      // 检查是否有不能操作的数据
      const invalidRows = selectedRows.filter(row => row.canCount === 0);
      if (invalidRows.length > 0) {
        this.$modal.msgError("存在不可操作的数据（canCount为0），请重新选择");
        return;
      }

      // 打开批量修改工序弹窗
      this.$refs.batchModifyGongxuForm.handleUpdate(ids);
    },

  }
};
</script>
